﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;

namespace Songhay.Data.GenericWeb.InputDefinitions
{
    using Songhay.Data.GenericWeb.DataTransferContracts;
    using Songhay.Xml;

    /// <summary>
    /// Defines input for the Web Keyword data transfer contract.
    /// </summary>
    public class InputWebKeyword : IWebKeyword
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InputWebKeyword"/> class.
        /// </summary>
        /// <param name="documentId">The document id.</param>
        /// <param name="keywordValue">The keyword value.</param>
        public InputWebKeyword(int? documentId, string keywordValue)
        {
            this._documentId = documentId;
            this._keywordValue = keywordValue;
        }

        /// <summary>
        /// Gets the list of Web keywords from the input.
        /// </summary>
        /// <param name="navigableInput">The navigable input.</param>
        /// <returns>Returns the list of Web Keyword data transfer contract objects.</returns>
        public static List<IWebKeyword> GetList(IXPathNavigable navigableInput)
        {
            bool throwExceptionForMissingDocumentIds = false;
            return GetList(navigableInput, throwExceptionForMissingDocumentIds);
        }

        /// <summary>
        /// Gets the list of Web keywords from the input.
        /// </summary>
        /// <param name="navigableInput">The navigable input.</param>
        /// <param name="throwExceptionForMissingDocumentIds">When <c>true</c> throw an exception for missing Document IDs.</param>
        /// <returns>Returns the list of Web Keyword data transfer contract objects.</returns>
        public static List<IWebKeyword> GetList(IXPathNavigable navigableInput, bool throwExceptionForMissingDocumentIds)
        {
            List<IWebKeyword> list = new List<IWebKeyword>();

            XPathNavigator nodeDocumentKeywords = XmlUtility.GetNavigableNode(navigableInput, "//DocumentKeywords");
            if(nodeDocumentKeywords != null)
            {
                XPathNavigator documentKeyword = null; object documentId = null; object keywordValue = null;
                foreach(IXPathNavigable n in nodeDocumentKeywords.SelectChildren(XPathNodeType.Element))
                {
                    documentKeyword = n.CreateNavigator();
                    documentId = XmlUtility.GetNodeValue(documentKeyword, "./DocumentId", throwExceptionForMissingDocumentIds);
                    keywordValue = XmlUtility.GetNodeValue(documentKeyword, "./KeywordValue", true);
                    if(!string.IsNullOrEmpty(FrameworkType.ParseString(keywordValue)))
                    {
                        list.Add(new InputWebKeyword(FrameworkType.ParseInt32(documentId), keywordValue.ToString()));
                    }
                }
            }

            return list;
        }

        #region IWebKeyword Members

        /// <summary>
        /// Gets or sets the Document ID.
        /// </summary>
        /// <value>The Document ID.</value>
        public int? DocumentId
        {
            get
            {
                return this._documentId;
            }
            set
            {
                this._documentId = value;
            }
        }

        /// <summary>
        /// Gets or sets the Keyword value.
        /// </summary>
        /// <value>The Keyword value.</value>
        public string KeywordValue
        {
            get
            {
                return this._keywordValue;
            }
            set
            {
                this._keywordValue = value;
            }
        }

        #endregion

        private int? _documentId;
        private string _keywordValue;
    }
}
